/* 
Copyright (c) 2004,2005, 2006, 2007, 2008 University of Illinois 

All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:

1. Redistributions of source code must retain the above copyright
   notice, this list of conditions and the following disclaimer.

2. Redistributions in binary form must reproduce the above copyright
   notice, this list of conditions and the following disclaimer in the
   documentation and/or other materials provided with the distribution.

3. The name of the author may not be used to endorse or promote products
   derived from this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
AccessibilityExtension.ns(function() { with (AccessibilityExtension.LIB) {

	var AE = AccessibilityExtension.ns;  
	AE.dialogName = "accessext-form-list-objects";
	AE.sortColumn = "list-header-attribute";

	function htmlObjectDisplayDetails() {}
	htmlObjectDisplayDetails.prototype = {
		dataElementNames : ['icita07', 'icita08','order', 'baseuri','typeDisplay','objectTitle'],
		columnHeaders : ['column.header.icita07.label', 'column.header.icita08.label','column.header.order.label','column.header.baseuri.label','column.header.type.label','column.header.text.label'],
		
		accessIssuesDataElementNames : ['altmissingapplet', 'altmissingembed', 'textmissing'],
		accessIssuesMessages : ['object.warnings.altmissingapplet', 'object.warnings.altmissingembed', 'object.warnings.textmissing'],
	}
	var htmlObjectDisplayDetails = new htmlObjectDisplayDetails();
	
	/*
	Mozilla provides the following:
	Object:
		attributes only in DOM, no JS
		does have textContent
		title
	Embed:
		no textcontent, listed as noembed after the element
		any free text (not part of a noembed) appears after the element
		
		noembed appears AFTER the embed, not as a child in DOM
		noembed is not guaranteed to be immediately following the embed
			<p>test</p>
			<noembed>no embed</noembed>
			would appear in that order outside of the embed
		DOM does display attributes, just no JS properties
		noembed appears as a htmldiv element, so dont expect anything special
		title
	Applet:
		textContent
		Children elements 
		alt and title
		param children
	*/
	
	function htmlAttributeDisplayDetails() {}
	htmlAttributeDisplayDetails.prototype = {
		dataElementNames : ['name','value'],
		columnHeaders : ['column.header.attribute.label','column.header.value.label'],
		
		accessIssuesDataElementNames : [],
		accessIssuesMessages : [],
	}
	var htmlAttributeDisplayDetails = new htmlAttributeDisplayDetails();
	
	//It may seem silly to have an object for this, but it is how the common list code works
	function htmlAttribute(node,name,value) {
		this.node = node;
		this.name = name;
		this.value = value;
	}
	htmlAttribute.prototype = {
		node : null,
		name : "",
		value : "",
		display: true,
		displayDetails: htmlAttributeDisplayDetails,
		getTreeItem : function (windoc) {
			var item = accesxt_generateTreeboxResults(document,this,this.displayDetails.dataElementNames);
			return item;
		}
	}
	
	function htmlObject(node,order) {
		this.node = node;
		this.order = order;
		this.parseNode();
	}
	htmlObject.prototype = {
		icita07 : "",
		icita08 : "",
		node : null,
		order : false,
		baseuri : "",
		objectTitle : "",
		displayText : "",
		type : "",//object, applet, embed
		typeDisplay : "",
		altmissingapplet : false, //used for applet elements only
		altmissingembed : false, //used for embed elements only
		textmissing : false, //used for obkject elements only
		valid : false,
		display: true,
		displayDetails: htmlObjectDisplayDetails,
		parameterNames : Array(),
		parameterValues : Array(),
		attributes : Array(),
		addParameter : function (name,value) {
			this.parameterNames[this.parameterNames.length] = name;
			this.parameterValues[this.parameterValues.length] = value;
		},
		parseNodeAsApplet : function () {
			this.objectTitle = GetValueFromAttributes(this.node,['title','alt'],false);
			var params = this.node.getElementsByTagName('param');
			for (var i = 0; i < params.length; i++) {
				if (params[i].hasAttribute('value') && params[i].hasAttribute('name')) {
					this.addParameter(params[i].getAttribute('name'),params[i].getAttribute('value'));
				}
			}
		},
		parseNodeAsObject : function () {
			this.objectTitle = GetValueFromAttributes(this.node,['title'],false);
		},
		findNoEmbedObject : function() {
			var walker = this.node.ownerDocument.createTreeWalker(this.node.ownerDocument.documentElement,NodeFilter.SHOW_ELEMENT,null,true);
			walker.currentNode = this.node;
			var curNode;
			var noEmbed = null;
			while (null != (curNode = walker.nextNode())) {
				if (curNode.tagName.toLowerCase() == 'noembed') {
					noEmbed = curNode;
					break;
				}
			}
		},
		parseNodeAsEmbed : function() {
			this.objectTitle = GetValueFromAttributes(this.node,['title'],false);
			// 10/19/07 SMF
			//<NOEMBED> has been deprecated in favor of the <object></object> and FireFox 2 does not support it.
			// http://webdesign.about.com/od/htmltags/p/bltags_noembed.htm
		},
		parseNode : function () {
			this.baseuri = getBaseURI(this.node);
			switch (this.node.tagName.toLowerCase()) {
				case 'object':
					this.type = 'object';
					this.parseNodeAsObject();
				break;
				case 'applet':
					this.type = 'applet';
					this.parseNodeAsApplet();	
				break;
				case 'embed':
					this.type = 'embed';
					this.parseNodeAsEmbed();
				break;
				default:
					this.valid = false;
					return false;
				break;
			}
			
			this.displayText = cleanSpaces(this.node.textContent);
			if (this.objectTitle == false ) {
				if (this.node.textContent) {
					this.objectTitle = cleanSpaces(this.node.textContent);
				} else {
					this.objectTitle = accessext_getBundleSingleString('none');
				}
			}
			if (this.objectTitle == accessext_getBundleSingleString('none') || this.objectTitle == '') {
				this.objectTitle = accessext_getBundleSingleString('none');
				if (this.type == 'object'){ this.textmissing = true; }
			}
			
			this.attributes = new Array();
			var altexists = false;
			var i;
			for (i = 0; i < this.node.attributes.length; i++) {
				this.attributes[this.attributes.length] = new htmlAttribute(this.node,this.node.attributes[i].nodeName,this.node.attributes[i].nodeValue);
				if (this.node.attributes[i].nodeName.toLowerCase() == 'alt') {altexists = true;}
			}
			if (altexists == false) {
				if (this.type == 'applet') { this.altmissingapplet = true;}
				else if (this.type == 'embed') {this.altmissingembed = true;}
			}
			this.displayOrder = this.order - 1;
			this.typeDisplay = accessext_getBundleSingleString('objects.type.' + this.type);
		},
		displayOrder : 0,
		getTreeItem : function (windoc) {
			return item = accesxt_generateTreeboxResults(document,this);
		},
		listDetailsInHTMLFormat : function () {
			return accesxt_generateHTMLTable('Object ' + (this.order+1) + ' Attributes', this.attributes);
		}
	}
AccessibilityExtension.LIB.extend(AccessibilityExtension.LIB,
{	
	newhtmlObject: function(node, order) { 
		var n = new htmlObject(node,order);
		return n; 
	},
	parseObject: function(node, loadArray) {
		var obj = new htmlObject(node,loadArray.length + 1);
		loadArray[loadArray.length] = obj;
	},
	
	ParseDocument: function(doc) {
		ParseDocumentForObjects(doc, AE.primaryarray);
	},
	
	ParseDocumentForObjects: function(docElement, loadArray) {
		var xp = "//embed|//object|//applet";
		var xpathResult = docElement.evaluate(xp, docElement, accessext_NSResolver, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE,null);
		for (var i = 0; i < xpathResult.snapshotLength; i++) {
			parseObject(xpathResult.snapshotItem(i), loadArray);
		}
		
		setMaxSeverityForArray(loadArray);
	},
	
	accessext_handle_treebox_main: function() {
		var textContentElement = document.getElementById("textbox-textcontent");
		if (null != textContentElement) {
			textContentElement.setAttribute("value","");
		}
		if (AE.treebox_main.currentIndex < 0 || AE.treebox_main.currentIndex >= AE.primaryarray.length) {
			return;
		}
			
		var findex = AE.treebox_main.currentIndex;
		var node = AE.primaryarray[findex].node;
		
		if (node == null || AE.mydoc == null) {
			if (node == null) {
				alert("Node is null");
			}
			return;
		}
		accessext_scrollToNodeCentered(node);
		accessext_highlightNode(node);
	
		var gotoButton = document.getElementById('button-goToURL');
		if (gotoButton) {
			gotoButton.disabled = true;
		}
		
		var textContentElement = document.getElementById("textbox-textcontent");
		if (null != textContentElement) {
			textContentElement.setAttribute("value",AE.primaryarray[findex].displayText);
		}
			
		accessext_setNoteText(findex);
		
		UpdateTreeView();
	},
	
	UpdateTreeViewMain: function() {
		var sc = document.getElementById(AE.sortColumn_main);
		if (sc == null) {
			accessext_consoleDump("listofobjects.UpdateTreeViewMain Could not find column: " + AE.sortColumn);
			return;
		}
	//Clear the children and create a new one
		var treeChildren = accessext_clearTreeItems(AE.treebox_main);
		accessext_clearTreeItems(AE.treebox);
		
		//Sort
		//First clear all directions
		var cols = AE.treebox_main.getElementsByTagName("treecols");
		var i;
		if (cols.length == 1) {
			for (i = 0; i < cols[0].childNodes.length; i ++) {
				cols[0].childNodes[i].setAttribute("AE.sortDirection","");
			}
		}
	
		sc.setAttribute("AE.sortDirection", AE.sortDirectionAscending_main ? "ascending" : "descending");
		switch (AE.sortColumn_main) {
			case "list-header-main-order":
				AE.sortProperty="order";
			break;
			case "list-header-main-text":
				AE.sortProperty="title";
			break;
			default:
				AE.sortProperty="order";
			break;
		}
		
		AE.primaryarray.sort(accessext_sortColumnElements);
		
		//Finally add elements to the list
		for (i = 0; i < AE.primaryarray.length; i ++) {
			var item = AE.primaryarray[i].getTreeItem(document);
			item.setAttribute("value", i);
			treeChildren.appendChild(item);
		}
	},
	
	UpdateTreeView: function() {
		var sc = document.getElementById(AE.sortColumn);
		if (sc == null) {
			accessext_consoleDump("listofobjects.UpdateTreeView Could not find column: " + AE.sortColumn);
			return;
		}
	//Clear the children and create a new one
		var treeChildren = accessext_clearTreeItems(AE.treebox);
	
		//Modifications for dual-tree view
		var findex = AE.treebox_main.currentIndex;
		if (findex < 0 || findex >= AE.primaryarray.length || findex == null) {
			AE.dataarray = new Array();
		} else {
			AE.dataarray = AE.primaryarray[findex].attributes;
		}	
		//Sort
		//First clear all directions
		var cols = AE.treebox.getElementsByTagName("treecols");
		var i;
		if (cols.length == 1) {
			for (i = 0; i < cols[0].childNodes.length; i ++)
				cols[0].childNodes[i].setAttribute("AE.sortDirection","");
		}
	
		sc.setAttribute("AE.sortDirection", AE.sortDirectionAscending ? "ascending" : "descending");
		switch (AE.sortColumn) {
			case "list-header-attribute":
				AE.sortProperty="name";
			break;
			case "list-header-value":
				AE.sortProperty="value";
			break;
			default:
				AE.sortProperty="attribute";
			break;
		}
		
		AE.dataarray.sort(accessext_sortColumnElements);
		
		for (i = 0; i < AE.dataarray.length; i ++) {
			var item = AE.dataarray[i].getTreeItem(document);
			item.setAttribute("value", i);
			treeChildren.appendChild(item);
		}
	},
	
	post_InitAccessibilityExt: function() {
		accessext_setNoteText(0);
	}
});
	if (AccessibilityExtension.LIB.InitAccessibilityExt) AccessibilityExtension.LIB.InitAccessibilityExt();		
}});		